
<h1><span class="yiyi-st" id="yiyi-12">numpy.fill_diagonal</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.fill_diagonal.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.fill_diagonal.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.fill_diagonal"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">fill_diagonal</code><span class="sig-paren">(</span><em>a</em>, <em>val</em>, <em>wrap=False</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/lib/index_tricks.py#L665-L776"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">填充给定数组的任何维数的主对角线。</span></p>
<p><span class="yiyi-st" id="yiyi-15">对于具有<code class="docutils literal"><span class="pre">a.ndim</span> <span class="pre">&gt;</span> <span class="pre">2</span></code>的数组<em class="xref py py-obj">a</em>具有索引<code class="docutils literal"><span class="pre">a [i，</span> <span class="pre">i，</span> <span class="pre">...，</span> <span class="pre">i]</span> / t5&gt;全部相同。</code></span><span class="yiyi-st" id="yiyi-16">此函数修改输入数组就地，不返回值。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-17">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-18"><strong>a</strong>：数组，至少为2-D。</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-19">数组的对角线将被填充，它被就地修改。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-20"><strong>val</strong>：标量</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-21">要写在对角线上的值，其类型必须与数组a的类型兼容。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-22"><strong>wrap</strong>：bool</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-23">对于NumPy版本的高矩阵高达1.6.2，对角线“包裹”N列后。</span><span class="yiyi-st" id="yiyi-24">你可以有这个选项的行为。</span><span class="yiyi-st" id="yiyi-25">这只影响高矩阵。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-26">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-27"><a class="reference internal" href="numpy.diag_indices.html#numpy.diag_indices" title="numpy.diag_indices"><code class="xref py py-obj docutils literal"><span class="pre">diag_indices</span></code></a>，<a class="reference internal" href="numpy.diag_indices_from.html#numpy.diag_indices_from" title="numpy.diag_indices_from"><code class="xref py py-obj docutils literal"><span class="pre">diag_indices_from</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-28">笔记</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-29"><span class="versionmodified">版本1.4.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-30">这个功能可以通过<a class="reference internal" href="numpy.diag_indices.html#numpy.diag_indices" title="numpy.diag_indices"><code class="xref py py-obj docutils literal"><span class="pre">diag_indices</span></code></a>获得，但在内部，这个版本使用一个快得多的实现，从来不构造索引和使用简单的切片。</span></p>
<p class="rubric"><span class="yiyi-st" id="yiyi-31">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[5, 0, 0],</span>
<span class="go">       [0, 5, 0],</span>
<span class="go">       [0, 0, 5]])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-32">相同的功能可以对4-D数组进行操作：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-33">为了清楚起见，我们只显示几个块：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="go">array([[4, 0, 0],</span>
<span class="go">       [0, 0, 0],</span>
<span class="go">       [0, 0, 0]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="go">array([[0, 0, 0],</span>
<span class="go">       [0, 4, 0],</span>
<span class="go">       [0, 0, 0]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
<span class="go">array([[0, 0, 0],</span>
<span class="go">       [0, 0, 0],</span>
<span class="go">       [0, 0, 4]])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-34">wrap选项仅影响高矩阵：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># tall matrices no wrap</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[4, 0, 0],</span>
<span class="go">       [0, 4, 0],</span>
<span class="go">       [0, 0, 4],</span>
<span class="go">       [0, 0, 0],</span>
<span class="go">       [0, 0, 0]])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># tall matrices wrap</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[4, 0, 0],</span>
<span class="go">       [0, 4, 0],</span>
<span class="go">       [0, 0, 4],</span>
<span class="go">       [0, 0, 0],</span>
<span class="go">       [4, 0, 0]])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># wide matrices</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fill_diagonal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">wrap</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[4, 0, 0, 0, 0],</span>
<span class="go">       [0, 4, 0, 0, 0],</span>
<span class="go">       [0, 0, 4, 0, 0]])</span>
</pre></div>
</div>
</dd></dl>
